package com.sudo.boot.service.sys_my.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sudo.boot.base.constant.SsoConstant;
import com.sudo.boot.base.constant.enums.EnumStatus;
import com.sudo.boot.base.exception.BizException;
import com.sudo.boot.mapper.sys.entity.SysRole;
import com.sudo.boot.mapper.sys.entity.SysUserRole;
import com.sudo.boot.mapper.sys.mapper.SysRoleMapper;
import com.sudo.boot.mapper.sys.mapper.SysUserRoleMapper;
import com.sudo.boot.mapper.sys.model.bo.my.MyRoleBoChange;
import com.sudo.boot.mapper.sys.model.vo.my.MyRoleVo;
import com.sudo.boot.satokensso.util.BootStpUtil;
import com.sudo.boot.service.sys_my.MyRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.sudo.boot.mapper.sys.entity.table.SysRoleTableDef.SYS_ROLE;
import static com.sudo.boot.mapper.sys.entity.table.SysUserRoleTableDef.SYS_USER_ROLE;

/**
 * 我的用户信息
 *
 * @author DayDayUp
 * @datetime 2023-11-09 16:43
 **/
@Service
public class MyRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements MyRoleService {
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public void changeRole(MyRoleBoChange bo) {
        String currUsername = BootStpUtil.getCurrUsername();
        // 查询角色
        SysRole sysRole = sysRoleMapper.selectOneById(bo.getRoleId());
        Assert.notNull(sysRole, "角色不存在");

        // 查询绑定关系
        QueryWrapper userRoleQuery = QueryWrapper.create()
                .where(SYS_USER_ROLE.ROLE_CODE.eq(sysRole.getRoleCode()))
                .and(SYS_USER_ROLE.USERNAME.eq(currUsername));
        SysUserRole sysUserRole = sysUserRoleMapper.selectOneByQuery(userRoleQuery);
        Assert.notNull(sysUserRole, "你还没有该角色");

        Assert.isTrue(sysRole.getStatus().equals(EnumStatus.ON.getCode()), "角色【" + sysRole.getRoleCode() + "】已禁用");

        // 设置当前角色缓存
        StpUtil.getSession().set(SsoConstant.currRoleCode, sysRole.getRoleCode());
    }

    @Override
    public List<MyRoleVo> getMyRoleList() {
        String currUsername = BootStpUtil.getCurrUsername();

        // 查询用户角色关联关系
        List<SysUserRole> userRoleList = sysUserRoleMapper.selectListByCondition(SYS_USER_ROLE.USERNAME.eq(currUsername));
        Assert.notEmpty(userRoleList, "暂无角色，请联系管理员");

        // 查询角色
        List<String> roleCodeList = userRoleList.stream().map(SysUserRole::getRoleCode).toList();
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where(SYS_ROLE.ROLE_CODE.in(roleCodeList));
        return sysRoleMapper.selectListByQueryAs(queryWrapper, MyRoleVo.class);
    }

    @Override
    public MyRoleVo getMyCurrRole() {
        String currRole = BootStpUtil.getCurrRole();
        if (!StringUtils.hasText(currRole)) {
            throw new BizException("当前角色不存在，请切换角色");
        }

        QueryWrapper queryWrapper = QueryWrapper.create()
                .where(SYS_ROLE.ROLE_CODE.eq(currRole));
        return sysRoleMapper.selectOneByQueryAs(queryWrapper, MyRoleVo.class);
    }
}
